package dq.study.concurrent.semaphore;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.Semaphore;

/**
 * @author dongqun
 * @version 1.0
 * @description 多进程多处理情况下使用synchronized关键字
 * @date 2019/9/5 19:03
 */
public class SemaphoreService5 implements ISemaphoreService{

    private Semaphore semaphore = new Semaphore(2,true);//多处理场景下信号量其实没起作用


    public void doSomething(){
        try {
            semaphore.acquire();
            doSomethingMain();
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author dongqun
     * @description 锁的是这个类
     * @date 2019/9/6 9:34
     * @param
     * @return
     */
    private static synchronized void doSomethingMain() throws InterruptedException {
        System.out.println(Thread.currentThread().getName()+":doSomething--start:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        Thread.sleep(2000);
        System.out.println(Thread.currentThread().getName()+":doSomething--end:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));

    }

}
